home *** CD-ROM | disk | FTP | other *** search
/ Power Bytes: Money & Finance / PowerBytes Money and Finance CD-ROM 01 / PowerBytes Money and Finance CD-ROM 01.iso / New Stuffed Files / PTUnit 1.1.sit / PTDemo.Pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-11-06  |  8.2 KB  |  326 lines  |  [TEXT/TPAS]

  1. Program PTDemo;
  2.  
  3. {$U-}
  4. {$R PTDemoRes}
  5. {$U PaintToolUnit}
  6.  
  7. Uses
  8.   MemTypes,QuickDraw,OSIntf,ToolIntf,PackIntf,
  9.   PaintToolUnit;
  10.   
  11. Const
  12.   AppleMenu     = 128;
  13.   FileMenu      = 129;
  14.   EditMenu      = 130;
  15.   ToolMenu      = 131;
  16.     itemPencil   = 1;
  17.     itemBucket   = 2;
  18.     itemBrush    = 3;
  19.     itemSpray    = 4;
  20.     itemLine     = 5;
  21.     itemEraser   = 6;
  22.     itemRect     = 8;
  23.     itemRectF    = 9;
  24.     itemRRect    = 10;
  25.     itemRRectF   = 11;
  26.     itemOval     = 12;
  27.     itemOvalF    = 13;
  28.     itemArc      = 14;
  29.     itemArcF     = 15;
  30.     itemRLine    = 17;
  31.     itemRLineF   = 18;
  32.   PatternMenu   = 132;
  33.   PenSizeMenu   = 133;
  34.   
  35. Var
  36.   myMenus       : array [AppleMenu..PenSizeMenu] of MenuHandle;
  37.   SprayCurs     : CursHandle;
  38.   CrossCurs     : CursHandle;
  39.   EraseCurs     : CursHandle;
  40.   theWindow     : WindowPtr;
  41.   GrowArea      : Rect;
  42.   CurrentTool   : Integer;
  43.   CurrentPat    : Integer;
  44.   CurrentSize   : Integer;
  45.  
  46. {###############################################################################}
  47.  
  48. Procedure ProcessMenu(codeWord : Longint);    { handle menu selections}
  49. Var
  50.   menuNum   : Integer;
  51.   itemNum   : Integer;
  52.   theStr    : Str255;
  53.   dummy     : Integer;
  54.   index     : Integer;
  55. Begin
  56.   if codeWord <> 0 then begin
  57.     menuNum := HiWord(codeWord);
  58.     itemNum := LoWord(codeWord);
  59.     case menuNum of { the different menus}
  60.       AppleMenu :
  61.         begin
  62.           if itemNum = 1 then {DoAbout}
  63.           else begin
  64.             GetItem(myMenus[AppleMenu],itemNum,theStr);
  65.             dummy:= OpenDeskAcc(theStr);
  66.           end;
  67.         end;
  68.       FileMenu : ExitToShell;
  69.       EditMenu : if NOT SystemEdit(itemNum - 1) then ;
  70.       ToolMenu :
  71.         begin
  72.           for index:= 1 to CountMItems(myMenus[ToolMenu]) do
  73.             CheckItem(myMenus[ToolMenu],index,false);
  74.           CheckItem(myMenus[ToolMenu],itemNum,true);
  75.           case itemNum of
  76.             itemPencil  : CurrentTool:= TPencil;
  77.             itemBucket  : CurrentTool:= TBucket;
  78.             itemBrush   : CurrentTool:= TBrush;
  79.             itemSpray   : CurrentTool:= TSpray;
  80.             itemLine    : CurrentTool:= TLine;
  81.             itemEraser  : CurrentTool:= TEraser;
  82.             itemRect    : CurrentTool:= TRect;
  83.             itemRectF   : CurrentTool:= TRectF;
  84.             itemRRect   : CurrentTool:= TRRect;
  85.             itemRRectF  : CurrentTool:= TRRectF;
  86.             itemOval    : CurrentTool:= TOval;
  87.             itemOvalF   : CurrentTool:= TOvalF;
  88.             itemArc     : CurrentTool:= TArc;
  89.             itemArcF    : CurrentTool:= TArcF;
  90.             itemRLine   : CurrentTool:= TRLine;
  91.             itemRLineF  : CurrentTool:= TRLineF;
  92.           end; {case}
  93.         end;
  94.       PatternMenu :
  95.         begin
  96.           CheckItem(myMenus[PatternMenu],CurrentPat,false);
  97.           CurrentPat:= itemNum;
  98.           CheckItem(myMenus[PatternMenu],CurrentPat,true);
  99.           SetPort(theWindow);
  100.           case CurrentPat of
  101.             1 : PenPat(white);
  102.             2 : PenPat(ltGray);
  103.             3 : PenPat(gray);
  104.             4 : PenPat(dkGray);
  105.             5 : PenPat(black);
  106.           end
  107.         end;
  108.       PenSizeMenu :
  109.         begin
  110.           CheckItem(myMenus[PenSizeMenu],CurrentSize,false);
  111.           CurrentSize:= itemNum;
  112.           CheckItem(myMenus[PenSizeMenu],CurrentSize,true);
  113.           SetPort(theWindow);
  114.           case CurrentSize of
  115.             1 : PenSize(1,1);
  116.             2 : PenSize(2,2);
  117.             3 : PenSize(2,4);
  118.             4 : PenSize(4,2);
  119.             5 : PenSize(5,5);
  120.             6 : PenSize(10,2);
  121.             7 : PenSize(2,10);
  122.             8 : PenSize(10,10);
  123.           end
  124.         end;
  125.     end; {case}
  126.     HiliteMenu(0);
  127.   end; {big if}
  128. End;
  129.  
  130. {###############################################################################}
  131.  
  132. Procedure DealWithMouseDowns(theEvent: EventRecord);
  133. Var
  134.   whichWindow   : WindowPtr;
  135.   mouseLoc      : Point;
  136.   windowLoc     : Integer;
  137.   position      : LongInt;
  138.   dummy         : Integer;
  139. Begin
  140.   mouseLoc:= theEvent.where;
  141.   windowLoc:= FindWindow(mouseLoc,whichWindow);
  142.   case windowLoc of
  143.     inMenuBar   : ProcessMenu(MenuSelect(mouseLoc));
  144.     inSysWindow : SystemClick(theEvent,whichWindow);
  145.     inDrag      : DragWindow(whichWindow,mouseLoc,screenBits.bounds);
  146.     inZoomIn,inZoomOut :
  147.       begin
  148.         if TrackBox(whichWindow,mouseLoc,windowLoc) then begin
  149.           SetPort(whichWindow);
  150.           ClipRect(whichWindow^.portRect);
  151.           EraseRect(whichWindow^.portRect);
  152.           ZoomWindow(whichWindow,windowLoc,true);
  153.           InvalRect(whichWindow^.portRect);
  154.         end;
  155.       end;
  156.     inGrow :
  157.       begin
  158.         position:= GrowWindow(whichWindow,mouseLoc,GrowArea);
  159.         if position <> 0 then begin
  160.           SizeWindow(whichWindow,loword(position),hiword(position),false);
  161.           SetPort(whichWindow);
  162.           InvalRect(whichWindow^.portRect);
  163.         end;
  164.       end;
  165.     inGoAway :
  166.       begin
  167.         if TrackGoAway(whichWindow,mouseLoc) then ExitToShell;
  168.       end;
  169.     inContent :
  170.       begin
  171.         if whichWindow <> FrontWindow then
  172.           SelectWindow(whichWindow)
  173.         else begin
  174.           SetPort(whichWindow);
  175.           GlobalToLocal(mouseLoc);
  176.           DoPaintTools(CurrentTool,mouseLoc,theEvent.modifiers);
  177.           if PTError = TUnImp then
  178.             dummy:= Alert(128,nil)
  179.           else if PTError <> noErr then sysbeep(1);
  180.         end;
  181.       end;
  182.   end;
  183. End;
  184.  
  185. Procedure DealWithKeyDowns(theEvent: EventRecord);
  186. Var
  187.   CharCode  : char;
  188. Begin
  189.   CharCode := CHR(BitAnd(theEvent.message,charCodeMask));
  190.   if BitAnd(theEvent.modifiers,CmdKey) = CmdKey
  191.     then ProcessMenu(MenuKey(CharCode));
  192. End;
  193.  
  194. Procedure DealWithActivates(theEvent: EventRecord);
  195. Var
  196.   TargetWindow    : WindowPtr;
  197. Begin
  198.   TargetWindow := WindowPtr(theEvent.message);
  199.   if Odd(theEvent.modifiers)
  200.     then SetPort(TargetWindow);
  201. End;
  202.  
  203. Procedure DealWithUpdates(theEvent: EventRecord);
  204. Var
  205.   UpDateWindow  : WindowPtr;
  206.   tempPort      : WindowPtr;
  207. Begin
  208.   UpDateWindow := WindowPtr(theEvent.message);
  209.   GetPort(tempPort);
  210.   SetPort(UpDateWindow); 
  211.   BeginUpDate(UpDateWindow);
  212.     ClipRect(UpDateWindow^.portRect);
  213.     EraseRect(UpDateWindow^.portRect);
  214.     PenSize(5,5);
  215.     FrameOval(UpDateWindow^.portRect);
  216.     PenSize(1,1);
  217.   EndUpDate(UpDateWindow);
  218.   SetPort(tempPort);
  219. End;
  220.  
  221. Procedure AdjustCursor;
  222. Var
  223.   where : Point;
  224. Begin
  225.   GetMouse(where);
  226.   if (theWindow = FrontWindow) AND PtInRect(where,theWindow^.portRect) then
  227.     case CurrentTool of
  228.       TSpray    : SetCursor(SprayCurs^^);
  229.       TLine     : SetCursor(CrossCurs^^);
  230.       TEraser   : SetCursor(EraseCurs^^);
  231.     end
  232.   else
  233.     InitCursor;
  234. End;
  235.  
  236. Procedure MainEventLoop;
  237. Var
  238.   Event        : EventRecord;
  239. Begin
  240.   repeat
  241.     AdjustCursor;
  242.     SystemTask;
  243.     if GetNextEvent(everyEvent, Event) then
  244.       case Event.what of
  245.         mouseDown   : DealWithMouseDowns(Event);
  246.         AutoKey     : DealWithKeyDowns(Event);
  247.         KeyDown     : DealWithKeyDowns(Event);
  248.         ActivateEvt : DealWithActivates(Event);
  249.         UpdateEvt   : DealWithUpdates(Event);
  250.       end; {case}
  251.   until FALSE;
  252. End;
  253.  
  254. {###############################################################################}
  255.  
  256. Procedure SetupMacintosh;
  257. Var
  258.   theWatch  : CursHandle;
  259. Begin
  260.   MaxApplZone;
  261.   MoreMasters;
  262.   MoreMasters;
  263.   MoreMasters;
  264.  
  265.   FlushEvents(everyEvent,0);
  266.  
  267.   InitGraf(@thePort);
  268.   InitFonts;
  269.   InitWindows;
  270.   InitMenus;
  271.   TEInit;
  272.   InitDialogs(nil);
  273.   
  274.   theWatch:= GetCursor(watchCursor);
  275.   SetCursor(theWatch^^);
  276. End;
  277.  
  278. Procedure SetupMenus;
  279. Var
  280.   index    : Integer;
  281. Begin
  282.   for index:= AppleMenu to PenSizeMenu do begin
  283.     myMenus[index] := GetMenu(index);
  284.     InsertMenu(myMenus[index],0);
  285.   end;
  286.   AddResMenu(myMenus[AppleMenu],'DRVR');
  287.   DrawMenuBar;
  288. End;
  289.  
  290. Procedure SetupStuff;
  291. Var
  292.   tempRect  : Rect;
  293.   version   : Integer;
  294. Begin
  295.   version:= InitPaintTools;
  296.  
  297.   with screenBits.bounds do begin
  298.     SetRect(GrowArea,200,200,right,bottom);
  299.     SetRect(tempRect,left+4,top+42,right-4,bottom-4);
  300.   end;
  301.     
  302.   theWindow:= NewWindow(nil,tempRect,'untitled',true,8,pointer(-1),true,0);
  303.   SetPort(theWindow);
  304.   
  305.   CurrentTool:= TPencil;
  306.   CurrentPat:= 5;
  307.   CurrentSize:= 1;
  308.   CheckItem(myMenus[ToolMenu],1,true);
  309.   CheckItem(myMenus[PatternMenu],CurrentPat,true);
  310.   CheckItem(myMenus[PenSizeMenu],CurrentSize,true);
  311.   
  312.   SprayCurs:= GetCursor(128);
  313.   CrossCurs:= GetCursor(crossCursor);
  314.   EraseCurs:= GetCursor(129);
  315. End;
  316.  
  317. Begin
  318.   SetupMacintosh;
  319.  
  320.   SetupMenus;
  321.   SetupStuff;
  322.   InitCursor;
  323.   
  324.   MainEventLoop;
  325. End.
  326.